<?php
namespace Plu\Library\Models\Platform\Reports;

use Phalcon\Mvc\Model;

class ReportListRoomPv extends Model {

    public function initialize() {
        $this->setReadConnectionService('db.report.slave');
    }

    public function getSource() {
        return "ReportListRoomPv";
    }

    /**
     * 根据游戏ID获取点播人气排行榜
     * @param gameid 游戏id
     * @param date   起始时间
     * @param $period 类型， 1 => 1天, 7 => 7天, 30 => 30天
     * @param $type 统计类型， 2为点播
     */
    public function getRoomsByGameId($gameids, $date, $period = 1, $offset = 0, $limit = 10, $type = 4) {
        $paramas = array(
            'Day'    => $date,
            'Period' => $period,
            'Type'   => $type
        );

        //如果游戏是数据的情况下
        if (!is_array($gameids)) {
            $gameids = array($gameids);
        }

        $sql = array();
        $len = count($gameids);
        for ($c = 0; $c < $len; $c++) {
            $p = "g{$c}";
            $sql[] = " g.GameId = :{$p}: ";
            $paramas[$p] = $gameids[$c];
        }

        $sql = '(' . join(' OR ', $sql) . ')';


        $phql = "SELECT p.Count, p.Day, r.Name as RoomName, r.Logo, r.RoomId, r.Domain, g.GameId FROM Plu\Library\Models\Platform\Reports\ReportListRoomPv as p LEFT JOIN Plu\Library\Models\Platform\Rooms\Room as r ON p.RoomId = r.RoomId LEFT JOIN Plu\Library\Models\Platform\Rooms\RoomOfGame as g ON g.RoomId = r.RoomId WHERE p.RoomId > 0 AND p.Day = :Day: AND p.Period = :Period: AND InfoType = :Type: AND $sql ORDER BY p.Count DESC LIMIT $offset, $limit";

        $modelsManager = $this->getDI()->get('modelsManager');
        $items = $modelsManager->executeQuery($phql, $paramas);

        return $items;
    }
}

?>
